/**
 * Copyright (c) 2018 软唐 All rights reserved.
 * <p>
 * https://www.softtown.cn
 * <p>
 * 版权所有，侵权必究！
 */

package com.softtown.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.softtown.common.constant.Constant;
import com.softtown.common.page.PageData;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.sys.dao.SysDictDao;
import com.softtown.modules.sys.dto.SysDictDTO;
import com.softtown.modules.sys.entity.SysDictEntity;
import com.softtown.modules.sys.service.SysDictService;
import com.softtown.modules.sys.vo.SysDictVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 数据字典
 *
 * @author Ding Ya Yong
 * @since 1.0.0
 */
@Service
public class SysDictServiceImpl extends BaseServiceImpl<SysDictDao, SysDictEntity> implements SysDictService {

    @Override
    public PageData<SysDictDTO> page(Map<String, Object> params) {
        QueryWrapper<SysDictEntity> wrapper = getWrapper(params);
        wrapper.eq("pid", Constant.DICT_ROOT);

        IPage<SysDictEntity> page = baseDao.selectPage(
                getPage(params, "sort", true),
                wrapper
        );

        return getPageData(page, SysDictDTO.class);
    }

    @Override
    public List<SysDictDTO> list(Map<String, Object> params) {
        List<SysDictEntity> entityList = baseDao.selectList(getWrapper(params));

        return ConvertUtils.sourceToTarget(entityList, SysDictDTO.class);
    }

    private QueryWrapper<SysDictEntity> getWrapper(Map<String, Object> params) {
        String pid = (String) params.get("pid");
        String dictType = (String) params.get("dictType");
        String dictName = (String) params.get("dictName");
        String dictValue = (String) params.get("dictValue");

        QueryWrapper<SysDictEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(pid != null, "pid", Long.parseLong(pid));
        wrapper.eq(StringUtils.isNotBlank(dictType), "dict_type", dictType);
        wrapper.like(StringUtils.isNotBlank(dictName), "dict_name", dictName);
        wrapper.like(StringUtils.isNotBlank(dictValue), "dict_value", dictValue);

        return wrapper;
    }

    @Override
    public SysDictDTO get(Long id) {
        SysDictEntity entity = baseDao.selectById(id);

        return ConvertUtils.sourceToTarget(entity, SysDictDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysDictDTO dto) {
        SysDictEntity entity = ConvertUtils.sourceToTarget(dto, SysDictEntity.class);

        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysDictDTO dto) {
        SysDictEntity entity = ConvertUtils.sourceToTarget(dto, SysDictEntity.class);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        //删除
        deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<SysDictEntity> getLanguageList() {
        return baseDao.getLanguageList();
    }

    @Override
    public List<SysDictEntity> getRuleTypeList() {
        return baseDao.getRuleTypeList();
    }

    @Override
    public List<SysDictEntity> getRuleStatusList() {
        return baseDao.getRuleStatusList();
    }

    @Override
    public List<SysDictEntity> getDocTypeList() {
        return baseDao.getDocTypeList();
    }

    /**
     * @param @param []
     * @return @return java.util.List<com.softtown.modules.sys.vo.SysDictVO>
     * @throws
     * @Title: getIsoCurrCdList
     * @Description: 查询出差业务币种字典信息值Service实现方法
     * @author fuming
     * @date 2019/7/27 11:20
     */
    @Override
    public List<SysDictVO> getIsoCurrCdList() {
        List<SysDictEntity> isoCurrCdList = baseDao.getIsoCurrCdList();
        return ConvertUtils.sourceToTarget(isoCurrCdList, SysDictVO.class);
    }

    /**
     * @param @param []
     * @return @return java.util.List<com.softtown.modules.sys.vo.SysDictVO>
     * @throws
     * @Title: getPayMethodList
     * @Description: 查询付款方式字典信息值Service实现方法
     * @author fuming
     * @date 2019/8/25 21:41
     */
    @Override
    public List<SysDictVO> getPayMethodList() {
        List<SysDictEntity> payMethodList = baseDao.getPayMethodList();
        return ConvertUtils.sourceToTarget(payMethodList, SysDictVO.class);
    }

    /**
     * @param @param []
     * @return @return java.util.List<com.softtown.modules.sys.vo.SysDictVO>
     * @throws
     * @Title: getSupList
     * @Description: 查询出差支持需求字典信息值Service实现方法
     * @author fuming
     * @date 2019/8/25 21:42
     */
    @Override
    public List<SysDictVO> getSupList() {
        List<SysDictEntity> supList = baseDao.getSupList();
        return ConvertUtils.sourceToTarget(supList, SysDictVO.class);
    }

    /**
     * 查询币种字典信息值
     *
     * @param
     * @return java.util.List<com.softtown.modules.sys.vo.SysDictVO>
     * @author fuming
     * @date 2019/10/13 13:45
     */
    @Override
    public List<SysDictVO> getIsoCurrCd() {
        return ConvertUtils.sourceToTarget(baseDao.getIsoCurrCdsList(), SysDictVO.class);
    }

    @Override
    public List<SysDictVO> getAprvMethodList() {
        return ConvertUtils.sourceToTarget(baseDao.getAprvMethodList(), SysDictVO.class);
    }

    /**
     * 查询借款类别
     * @param
     * @return java.util.List<com.softtown.modules.sys.vo.SysDictVO>
     * @author fuming
     * @date 2019/10/18 18:58
     */
    @Override
    public List<SysDictVO> selectAdvType() {
        return ConvertUtils.sourceToTarget(baseDao.selectAdvTypeList(), SysDictVO.class);
    }

    @Override
    public List<SysDictVO> getInvoiceTypeList() {
        return ConvertUtils.sourceToTarget(baseDao.getInvoiceTypeList(), SysDictVO.class);
    }
}